package com.chebaohui.iqs.service.impl.deal.bihu.profess;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.CompanyCode;
import com.chebaohui.iqs.config.InsuranceCode;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.Result;
import com.chebaohui.iqs.entity.base.Cars;
import com.chebaohui.iqs.entity.base.Company;
import com.chebaohui.iqs.entity.base.Insurance;
import com.chebaohui.iqs.entity.base.License;
import com.chebaohui.iqs.entity.base.Order;
import com.chebaohui.iqs.entity.base.Owner;
import com.chebaohui.iqs.entity.req.LastYearPolicyReq;
import com.chebaohui.iqs.entity.res.LastYearPolicyRes;
import com.chebaohui.iqs.service.impl.deal.bihu.entity.req.GetreInfo;
import com.chebaohui.iqs.service.impl.deal.bihu.num.OneInterface;
import com.chebaohui.iqs.service.impl.deal.bihu.utils.EnumUtil;
import com.chebaohui.iqs.service.impl.deal.util.StringUtil;
import com.chebaohui.iqs.service.impl.deal.util.TimeBase;

/**
 * 往年保单信息查询--壁虎接口处理类
 * 
 * @author xielonghai
 *
 */
public class BiHuPolicyCheck {
	/**
	 * log:日志输出
	 */
	private Log log = LogFactory.getLog(BiHuPolicyCheck.class);
	// 壁虎接口1查詢結果
	private JSONObject result;

	LastYearPolicyRes policyResult;

	public BiHuPolicyCheck() {
		this.policyResult = new LastYearPolicyRes();
	}

	/**
	 * 壁虎接口1，查询以及返回结果处理
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public Result<LastYearPolicyRes> policy(LastYearPolicyReq param) throws Exception {
		try {
			log.info(param.getLicenceNo()+",1开始进入《壁虎接口1》查询往年保单...");
			GetreInfo bihuone = setCanShu(param);

			result = new OneInterface().one(bihuone);

			log.info(param.getLicenceNo()+",2处理《壁虎接口1》往年保单查询结果...");
			if (null == result && result.isEmpty()) {
				return new Result<LastYearPolicyRes>(ResultCode.N04, "接口返回空");
			}

			if (!"1".equals(result.getString("BusinessStatus")))
				return new Result<LastYearPolicyRes>(ResultCode.N04, result.getString("StatusMessage"));

			Integer Source = result.getJSONObject("SaveQuote").getInteger("Source");

			if (null == Source || Source < 1)
				return new Result<LastYearPolicyRes>(ResultCode.N04, "该车没有续保信息");

			log.info(param.getLicenceNo()+",3《壁虎接口1》往年保单查询结果,开始设置返回保单信息...");
			// 设置返回保单信息
			setCompany();
			setLicense();
			setOrder();
			setOwner();
			setCars();
			setInsurance();
			log.info(param.getLicenceNo()+",4《壁虎接口1》往年保单查询结果,设置返回保单信息完成...");
			return new Result<LastYearPolicyRes>(ResultCode.SUCCESS, policyResult);
		} catch (Exception e) {
			log.error(param.getLicenceNo()+",《壁虎接口1》往年保单查询处理程序异常:" + e.getMessage());
			throw new Exception("往年保单查询处理异常:"+e.getMessage());
			//return new Result<LastYearPolicyRes>(ResultCode.N02, "程序异常policy:" + e.getMessage());
		} finally {
			log.info(param.getLicenceNo()+",5《壁虎接口1》往年保单查询、处理完成...");
		}
	}

	/**
	 * 设置好壁虎一号接口所需参数
	 * 
	 * @param param
	 * @return
	 */
	private GetreInfo setCanShu(LastYearPolicyReq param) {
		GetreInfo bihuone = new GetreInfo();
		bihuone.setLicenseNo(param.getLicenceNo());
		bihuone.setCanShowNo(1);
		bihuone.setCanShowExhaustScale(1);
		bihuone.setShowXiuLiChangType(1);
		bihuone.setTimeFormat(1);
		bihuone.setShowAutoMoldCode(1);

		if (!StringUtil.isNulls(param.getFrameNo()))
			bihuone.setCarVin(param.getFrameNo());
		if (!StringUtil.isNulls(param.getEngineNo()))
			bihuone.setEngineNo(param.getEngineNo());
		return bihuone;
	}

	/**
	 * 设置保险公司
	 * 
	 */
	private void setCompany() throws Exception {
		Company company = new Company();
		String string = result.getJSONObject("SaveQuote").getString("Source");
		CompanyCode code = getCompany(string);
		company.setCompanyCode(code.getCode());
		company.setModelCode(result.getJSONObject("UserInfo").getString("AutoMoldCode"));
		/*
		 * 该参数不会返回值，没用 company.setRelyCompanyCode("relyCompanyCode");
		 */
		policyResult.setCompany(company);
	}

	/**
	 * 转化保险公司的值为code 如果有多家 只取最小的值
	 * 
	 * @param string
	 * @return
	 * @throws Exception
	 */
	private CompanyCode getCompany(String string) throws Exception {
		CompanyCode source = null;
		List<Double> list = EnumUtil.getCombination(Double.valueOf(string));
		Double min = Collections.min(list);
		source = EnumUtil.getSource(min);
		return source;
	}

	/**
	 * 设置行驶证信息
	 * 
	 */
	private void setLicense() {
		License license = new License();
		JSONObject ls = result.getJSONObject("UserInfo");
		license.setEngineNo(ls.getString("EngineNo"));
		license.setRegisteredTime(ls.getString("RegisterDate"));
		license.setFrameNo(ls.getString("CarVin"));
		license.setLableNo(ls.getString("ModleName"));
		license.setLicenceNo(ls.getString("LicenseNo"));
		license.setOwnerName(ls.getString("LicenseOwner"));
		license.setTransfer("00");
		policyResult.setLicense(license);
	}

	/**
	 * 设置车主信息
	 */
	private void setOwner() {
		Owner owner = new Owner();
		JSONObject ow = result.getJSONObject("UserInfo");
		owner.setIdCard(ow.getString("CredentislasNum"));
		owner.setName(ow.getString("LicenseOwner"));
		policyResult.setOwner(owner);
	}

	/**
	 * 设置订单信息<车险上年终保时间，以及起保时间>
	 */
	private void setOrder() throws Exception{
		Order order = new Order();
		JSONObject od = result.getJSONObject("UserInfo");
		// 商业险终保时间
		order.setBusEndTime(od.getString("NextBusinessStartDate"));
		// 交强险终保时间
		order.setTraEndTime(od.getString("NextForceStartDate"));
		policyResult.setOrder(order);
	}

	/**
	 * 设置车辆信息
	 */
	private void setCars() {
		Cars car = new Cars();
		car.setExhausMeasur(result.getJSONObject("UserInfo").getString("ExhaustScale"));
		car.setModelCode(result.getJSONObject("UserInfo").getString("AutoMoldCode"));
		car.setModelName(result.getJSONObject("UserInfo").getString("ModleName"));
		car.setOption("");// 车型描述
		car.setSaleDate("");// 年款
		car.setPurchase(result.getJSONObject("UserInfo").getString("PurchasePrice"));
		car.setRemark(result.getJSONObject("UserInfo").getString("CarUsedType"));// 款型
		car.setSeats(result.getJSONObject("UserInfo").getIntValue("SeatCount"));// 座位数
		policyResult.setCar(car);

	}

	/**
	 * 设置车险
	 * @throws Exception 
	 */
	private void setInsurance() throws Exception {
		JSONObject ci = result.getJSONObject("SaveQuote");
		
		List<Insurance> listI = new ArrayList<Insurance>();
		
		String string = result.getJSONObject("UserInfo").getString("ForceExpireDate");

		if(!StringUtil.isNulls(string)){
			if(TimeBase.isForce(string)){//交强险
				Insurance tp = new Insurance();
				tp.setCode(InsuranceCode.TP.getCode());
				tp.setAuount("1");
				tp.setIndemnity("false");
				listI.add(tp);
			}
		}
		if (null != ci.getDouble("CheSun") && ci.getDouble("CheSun") > 0) {// 车损险
			Insurance cs = new Insurance();
			cs.setCode(InsuranceCode.BL.getCode());
			cs.setAuount(ci.getString("CheSun"));
			if (ci.getDouble("BuJiMianCheSun") > 0) {
				cs.setIndemnityPrice(ci.getString("BuJiMianCheSun"));
				cs.setIndemnity("true");
			} else {
				cs.setIndemnity("false");
			}
			listI.add(cs);
		}
		if (null != ci.getDouble("SanZhe") && ci.getDouble("SanZhe") > 0) {// 第三责任险
			Insurance sz = new Insurance();
			sz.setCode(InsuranceCode.BTR.getCode());
			sz.setAuount(ci.getString("SanZhe"));
			if (ci.getDouble("BuJiMianSanZhe") > 0) {
				sz.setIndemnityPrice(ci.getString("BuJiMianSanZhe"));
				sz.setIndemnity("true");
			} else {
				sz.setIndemnity("false");
			}
			listI.add(sz);
		}
		if (null != ci.getDouble("DaoQiang") && ci.getDouble("DaoQiang") > 0) {// 盗抢险
			Insurance dq = new Insurance();
			dq.setCode(InsuranceCode.BT.getCode());
			dq.setAuount(ci.getString("DaoQiang"));
			if (ci.getDouble("BuJiMianDaoQiang") > 0) {
				dq.setIndemnityPrice(ci.getString("BuJiMianDaoQiang"));
				dq.setIndemnity("true");
			} else {
				dq.setIndemnity("false");
			}
			listI.add(dq);
		}
		if (null != ci.getDouble("SiJi") && ci.getDouble("SiJi") > 0) {// 司机险
			Insurance sj = new Insurance();
			sj.setCode(InsuranceCode.BD.getCode());
			sj.setAuount(ci.getString("SiJi"));
			if (ci.getDouble("BuJiMianSiJi") > 0) {
				sj.setIndemnityPrice(ci.getString("BuJiMianSiJi"));
				sj.setIndemnity("true");
			} else {
				sj.setIndemnity("false");
			}
			listI.add(sj);
		}
		if (null != ci.getDouble("ChengKe") && ci.getDouble("ChengKe") > 0) {// 乘客险
			Insurance ck = new Insurance();
			ck.setCode(InsuranceCode.BP.getCode());
			ck.setAuount(ci.getString("ChengKe"));
			if (ci.getDouble("BuJiMianChengKe") > 0) {
				ck.setIndemnityPrice(ci.getString("BuJiMianChengKe"));
				ck.setIndemnity("true");
			} else {
				ck.setIndemnity("false");
			}
			listI.add(ck);
		}
		if (null != ci.getDouble("BoLi") && ci.getDouble("BoLi") > 0) {// 玻璃险
			Insurance bl = new Insurance();
			bl.setCode(InsuranceCode.BG.getCode());
			bl.setAuount(ci.getString("BoLi"));
			bl.setType("00");
			bl.setIndemnity("false");
			listI.add(bl);
		}
		if (null != ci.getDouble("HcSanFangTeYue") && ci.getDouble("HcSanFangTeYue") > 0) {//第三方特约险
			Insurance btt = new Insurance();
			btt.setCode(InsuranceCode.BTT.getCode());
			btt.setAuount(ci.getString("HcSanFangTeYue"));
			btt.setIndemnity("false");
			listI.add(btt);
		}
		if (null != ci.getDouble("HuaHen") && ci.getDouble("HuaHen") > 0) {// 划痕险
			Insurance hh = new Insurance();
			hh.setCode(InsuranceCode.BN.getCode());
			hh.setAuount(ci.getString("HuaHen"));
			if (ci.getDouble("BuJiMianHuaHen") > 0) {
				hh.setIndemnityPrice(ci.getString("BuJiMianHuaHen"));
				hh.setIndemnity("true");
			} else {
				hh.setIndemnity("false");
			}
			listI.add(hh);
		}
		if (null != ci.getDouble("SheShui") && ci.getDouble("SheShui") > 0) {// 涉水险
			Insurance ss = new Insurance();
			ss.setCode(InsuranceCode.BW.getCode());
			ss.setAuount(ci.getString("SheShui"));
			if (ci.getDouble("BuJiMianSheShui") > 0) {
				ss.setIndemnityPrice(ci.getString("BuJiMianSheShui"));
				ss.setIndemnity("true");
			} else {
				ss.setIndemnity("false");
			}
			listI.add(ss);
		}
		if (null != ci.getDouble("ZiRan") && ci.getDouble("ZiRan") > 0) {// 自燃险
			Insurance zr = new Insurance();
			zr.setCode(InsuranceCode.BNP.getCode());
			zr.setAuount(ci.getString("ZiRan"));
			if (ci.getDouble("BuJiMianZiRan") > 0) {
				zr.setIndemnityPrice(ci.getString("BuJiMianZiRan"));
				zr.setIndemnity("true");
			} else {
				zr.setIndemnity("false");
			}
			listI.add(zr);
		}
		if (!StringUtil.isNulls(ci.getString("HcXiuLiChang")) && ci.getDouble("HcXiuLiChang") > 0) {// 指定修理厂险
			Insurance hxlc = new Insurance();
			hxlc.setCode(InsuranceCode.BR.getCode());
			hxlc.setAuount(ci.getString("HcXiuLiChang"));
			if (ci.getString("HcXiuLiChangType").equals("0")) {
				hxlc.setType("00");
				hxlc.setPate("30");
			}
			if (ci.getString("HcXiuLiChangType").equals("1")) {
				hxlc.setType("01");
				hxlc.setPate("60");
			}
			listI.add(hxlc);
		}
		policyResult.setInsuranceList(listI);
	}

}
